//
//  WPPaySuccessAlertView.m
//  ZhongChouPingTai
//
//  Created by WeiPan on 16/4/10.
//  Copyright © 2016年 WPJoe. All rights reserved.
//

#import "WPPaySuccessAlertView.h"

#define DURATION 0.5f

@interface WPPaySuccessAlertView ()
@property (nonatomic, strong) NSString * detail;/**< 成功内容 */
@property (nonatomic, strong) UIView * redBackView;/**< 红色背景图 */
@property (nonatomic, strong) UIImageView * hookImageView;/**< 勾 */
@property (nonatomic, strong) UILabel * titleLabel;/**< 标题文本 */
@property (nonatomic, strong) UIButton * cancelbtn;/**< 取消按钮 */
@property (nonatomic , strong) UIView * mask;/**< 遮罩视图 */
@property (nonatomic, strong) UILabel * detailTitleLabel;/**< 恭喜文本 */
@property (nonatomic, strong) UILabel * detailLabel;/**< 成功内容文本 */
@property (nonatomic, strong) UILabel * topLabel;/**< 提醒文本 */
@property (nonatomic, strong) UIButton * showRightBtn;/**< 查看按钮 */
@property (nonatomic, copy) void (^complete)(NSString *result);/**< 结果block */
- (void)initializeAppearance;/**< 一般界面初始化 */
@end
@implementation WPPaySuccessAlertView

+ (WPPaySuccessAlertView *)showViewWithSize:(CGRect)rect andWithDetail:(NSString *)detail WithComplete:(void (^)(NSString *))complete {
    WPPaySuccessAlertView * view = [[WPPaySuccessAlertView alloc]initWithFrame:rect withDetail:detail andWithComplete:complete];
    [view show];
    return view;
}
- (instancetype)initWithFrame:(CGRect)frame withDetail:(NSString *)detail andWithComplete:(void (^)(NSString *))complete {
    if ([super initWithFrame:frame]) {
        _detail = detail;
        _complete = complete;
        [self initializeAppearance];
    }
    return self;
}
#pragma mark - init
- (void)initializeAppearance {
    if (self.frame.size.height == 0) {
        self.bounds = CGRectMake(0, 0, SCREEN_SIZE.width*0.8, SCREEN_SIZE.height * 0.7);
        self.center = [UIApplication sharedApplication].keyWindow.center;
    }
    self.backgroundColor = [UIColor whiteColor];
    self.layer.cornerRadius = 10;
    self.layer.masksToBounds = YES;
    [self addSubview:self.redBackView];
    [self addSubview:self.hookImageView];
    [self addSubview:self.titleLabel];
    [self addSubview:self.cancelbtn];
    [self addSubview:self.detailTitleLabel];
    [self addSubview:self.detailLabel];
    [self addSubview:self.topLabel];
    [self addSubview:self.showRightBtn];
}
#pragma mark - responds event
- (void)respondsToCancelBtn {
    [self hide];
}
- (void)respondsToShowRightBtn {
    [self hide];
    _complete(@"查看权益");
}

#pragma mark 显示\隐藏动画
- (void)show {
    [[UIApplication sharedApplication].keyWindow addSubview:self.mask];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    self.alpha = 0.0;
    [UIView animateWithDuration:DURATION animations:^{
        self.alpha = 1.0;
    } completion:nil];
}

- (void)hide {
    [UIView animateWithDuration:DURATION animations:^{
        self.mask.alpha = 0.0;
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        [self.mask removeFromSuperview];
    }];
}
#pragma mark - getter
- (UIView *)mask {
    if (!_mask) {
        _mask = ({
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, SCREEN_SIZE.height)];
            view.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.3];
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
            [view addGestureRecognizer:tap];
            view;
        });
    }
    return _mask;
}
- (UIView *)redBackView {
    if (!_redBackView) {
        _redBackView = ({
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 270*DHFlexibleHorizontalMutiplier(), 90*DHFlexibleHorizontalMutiplier())];
            view.backgroundColor = COLOR_RGB(234, 27, 47, 1);
            view;
        });
    }
    return _redBackView;
}
- (UIImageView *)hookImageView {
    if (!_hookImageView) {
        _hookImageView = ({
            UIImageView * view = [[UIImageView alloc]initWithFrame:CGRectMake(80*DHFlexibleHorizontalMutiplier(), 35*DHFlexibleHorizontalMutiplier(), 20*DHFlexibleHorizontalMutiplier(), 20*DHFlexibleHorizontalMutiplier())];
            view.image = IMAGE_NAME(@"jycg");
            view;
        });
    }
    return _hookImageView;
}
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = ({
            UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(120*DHFlexibleHorizontalMutiplier(), 25*DHFlexibleHorizontalMutiplier(), 200, 40*DHFlexibleHorizontalMutiplier())];
            label.font = [UIFont systemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            label.textColor = [UIColor whiteColor];
            label.text = @"交易成功";
            label;
        });
    }
    return _titleLabel;
}
- (UIButton *)cancelbtn {
    if (!_cancelbtn) {
        _cancelbtn = ({
            UIButton * btn =[ UIButton buttonWithType:UIButtonTypeCustom];
            btn.bounds = CGRectMake(0, 0, 22*DHFlexibleHorizontalMutiplier(), 22*DHFlexibleHorizontalMutiplier());
            btn.center = CGPointMake(CGRectGetMaxX(self.bounds)-21*DHFlexibleHorizontalMutiplier(), 20*DHFlexibleHorizontalMutiplier());
            [btn setImage:[UIImage imageNamed:@"alertViewCancel"] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(respondsToCancelBtn) forControlEvents:UIControlEventTouchUpInside];
            btn;
        });
    }
    return _cancelbtn;
}
- (UILabel *)detailTitleLabel {
    if (!_detailTitleLabel) {
        _detailTitleLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(10*DHFlexibleHorizontalMutiplier(), 110*DHFlexibleHorizontalMutiplier(), 200, 30*DHFlexibleHorizontalMutiplier())];
            lab.font = [UIFont systemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            lab.textColor = COLOR_RGB(174, 174, 174, 1);
            lab.text = @"恭喜您获得";
            lab;
        });
    }
    return _detailTitleLabel;
}
- (UILabel *)detailLabel {
    if (!_detailLabel) {
        _detailLabel = ({
            UILabel  * lab = [[UILabel alloc]initWithFrame:CGRectMake(10*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.detailTitleLabel.frame), 250*DHFlexibleHorizontalMutiplier(), 50*DHFlexibleHorizontalMutiplier())];
            lab.numberOfLines = 0;
            lab.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            lab.textColor = COLOR_RGB(103, 103, 103, 1);
            lab.text = _detail;
            lab;
        });
    }
    return _detailLabel;
}
- (UILabel *)topLabel {
    if (!_topLabel) {
        _topLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(10*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.detailLabel.frame), 250*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier())];
            lab.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            lab.textColor = COLOR_RGB(174, 174, 174, 1);
            lab.text = @"请您注意演唱会详细信息及自助取票时间";
            lab;
        });
    }
    return _topLabel;
}
- (UIButton *)showRightBtn {
    if (!_showRightBtn) {
        _showRightBtn = ({
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(50*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.bounds) - 40*DHFlexibleHorizontalMutiplier(), 170*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier());
            btn.backgroundColor = COLOR_RGB(234, 27, 47, 1);
            btn.layer.cornerRadius = 5*DHFlexibleHorizontalMutiplier();
            [btn setTitle:@"查看权益" forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            [btn addTarget:self action:@selector(respondsToShowRightBtn) forControlEvents:UIControlEventTouchUpInside];
            btn;

        });
    }
    return _showRightBtn;
}
@end
